//
//  LSExpertRankCell.m
//  LookScore
//
//  Created by wr on 2018/5/5.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSExpertRankCell.h"
#import "LSExpertsCollectionViewCell.h"
#import "LSExpertRankModel.h"
#pragma mark 🔥 LSExpertRankCell
@interface LSExpertRankCell()

@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) UILabel *ExpertName;
@property (nonatomic, strong) UILabel *ExpertInfo;
@property (nonatomic, strong) UIImageView *redBox;
@property (nonatomic, strong) UILabel *redLable;
@property (nonatomic, strong) UIButton *followBtn;
@end
@implementation LSExpertRankCell
//- (UIImageView *)rankImageView{
//    if (!_rankImageView) {
//        _rankImageView = [[UIImageView alloc] init];
//    }
//    return _rankImageView;
//}
//
//- (UIImageView *)headImageView{
//    if (!_headImageView) {
//        _headImageView = [[UIImageView alloc] init];
//        _headImageView.image = [UIImage imageNamed:@"HeadImage"];
//    }
//    return _headImageView;
//}
//
//- (UILabel *)ExpertName{
//    if (!_ExpertName) {
//        _ExpertName = [[UILabel alloc] init];
//        _ExpertName.font = LSFontSize(15,LSScreenWidth);
//        _ExpertName.textColor = LSSubColor5D5D5D;
//    }
//    return _ExpertName;
//}
//
//- (UILabel *)ExpertInfo{
//    if (!_ExpertInfo) {
//        _ExpertInfo = [[UILabel alloc] init];
//        _ExpertInfo.font = LSFontSize(13,LSScreenWidth);
//        _ExpertInfo.textColor = LSTitleGrayColor2;
//    }
//    return _ExpertInfo;
//}
//
//- (UIImageView *)redBox{
//    if (!_redBox) {
//        _redBox = [[UIImageView alloc] init];
//        _redBox.image = [UIImage imageNamed:@"RedBox"];
//    }
//    return _redBox;
//}
//
//- (UILabel *)redLable{
//    if (!_redLable) {
//        _redLable = [[UILabel alloc] init];
//        _redLable.textAlignment = NSTextAlignmentCenter;
//        _redLable.font = LSFontSize(12,LSScreenWidth);
//        _redLable.textColor = [UIColor redColor];
//    }
//    return _redLable;
//}
//
//- (UIButton *)followBtn{
//    if (!_followBtn) {
//        _followBtn = [[UIButton alloc] init];
//        [_followBtn setImage:[UIImage imageNamed:@"Follow"] forState:UIControlStateNormal];
//        [_followBtn setImage:[UIImage imageNamed:@"Followed"] forState:UIControlStateSelected];
//        [_followBtn addTarget:self action:@selector(clickFollowBtn:) forControlEvents:UIControlEventTouchUpInside];
//    }
//    return _followBtn;
//}
+(LSExpertRankCell *)ExpertRankCellCellByTableView:(UITableView *)tableView{
    LSExpertRankCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass(self)];
    if (!cell) {
        cell=[[LSExpertRankCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(self)];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }return cell;
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addContentView];
    }
    
    return self;
}

-(void)addContentView{
    
    UIImageView *rankImageView = [[UIImageView alloc] init];
    self.rankImageView = rankImageView;
    [self.contentView addSubview:self.rankImageView];
    self.rankImageView.sd_layout
    .leftEqualToView(self.contentView).offset(LSAutoVlaue(20, LSScreenWidth))
    .centerYEqualToView(self.contentView)
    .heightIs(LSAutoVlaue(30, LSScreenHeight))
    .widthIs(LSAutoVlaue(25, LSScreenWidth));
    
    
    UIImageView *headImageView = [[UIImageView alloc] init];
//    headImageView.image = [UIImage imageNamed:@"HeadImage"];
    self.headImageView = headImageView;
    [self.contentView addSubview:self.headImageView];
    self.headImageView.sd_layout
    .leftSpaceToView(self.rankImageView, LSAutoVlaue(20, LSScreenWidth))
    .centerYEqualToView(self.contentView)
    .widthIs(LSAutoVlaue(50, LSScreenWidth))
    .heightIs(LSAutoVlaue(50, LSScreenHeight));
    
    
    UILabel *ExpertName = [[UILabel alloc] init];
    ExpertName.font = LSFontSize(15,LSScreenWidth);
    ExpertName.textColor = LSSubColor5D5D5D;
//    self.ExpertName.text = @"足彩专家";
    self.ExpertName = ExpertName;
    [self.contentView addSubview:self.ExpertName];
    self.ExpertName.sd_layout
    .leftSpaceToView(self.headImageView, LSAutoVlaue(20, LSScreenWidth))
    .centerYEqualToView(self.contentView).offset(-LSAutoVlaue(10, LSScreenHeight))
    .widthIs(LSAutoVlaue(80, LSScreenWidth))
    .autoHeightRatio(0);
    
    UILabel *ExpertInfo = [[UILabel alloc] init];
    ExpertInfo.font = LSFontSize(13,LSScreenWidth);
    ExpertInfo.textColor = LSTitleGrayColor2;
//    self.ExpertInfo.text = @"足彩分析师";
    self.ExpertInfo = ExpertInfo;
    [self.contentView addSubview:self.ExpertInfo];
    self.ExpertInfo.sd_layout
    .leftEqualToView(self.ExpertName)
    .topSpaceToView(self.ExpertName, LSAutoVlaue(5, LSScreenHeight))
    .widthIs(LSAutoVlaue(80, LSScreenWidth))
    .autoHeightRatio(0);
    
    
    UIImageView *redBox = [[UIImageView alloc] init];
    redBox.image = [UIImage imageNamed:@"RedBox"];
    self.redBox = redBox;
    [self.contentView addSubview:self.redBox];
    self.redBox.sd_layout
    .leftSpaceToView(self.ExpertName, LSAutoVlaue(20, LSScreenWidth))
    .centerYEqualToView(self.contentView)
    .widthIs(LSAutoVlaue(70, LSScreenWidth))
    .heightIs(LSAutoVlaue(33, LSScreenHeight));
    
    
    UILabel *redLable = [[UILabel alloc] init];
    redLable.textAlignment = NSTextAlignmentCenter;
    redLable.font = LSFontSize(12,LSScreenWidth);
    redLable.textColor = [UIColor redColor];
    redLable.numberOfLines = 2;
//    self.redLable.text = @"10连胜";
    self.redLable = redLable;
    [self.redBox addSubview:self.redLable];
    self.redLable.sd_layout
    .centerYEqualToView(self.redBox)
    .centerXEqualToView(self.redBox)
    .widthIs(LSAutoVlaue(60, LSScreenWidth))
    .heightIs(LSAutoVlaue(30, LSScreenHeight));
    
    
    UIButton *followBtn = [[UIButton alloc] init];
    [followBtn setImage:[UIImage imageNamed:@"Follow"] forState:UIControlStateNormal];
    [followBtn setImage:[UIImage imageNamed:@"Followed"] forState:UIControlStateSelected];
    [followBtn addTarget:self action:@selector(clickFollowBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.followBtn = followBtn;
    [self.contentView addSubview:self.followBtn];
    self.followBtn.sd_layout
    .leftSpaceToView(self.redBox, LSAutoVlaue(20, LSScreenWidth))
    .rightEqualToView(self.contentView).offset(-LSAutoVlaue(20, LSScreenWidth))
    .centerYEqualToView(self.redBox)
    .heightIs(LSAutoVlaue(30, LSScreenHeight));
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = LSSubGrayColorf7;
    [self.contentView addSubview:line];
    line.sd_layout
    .leftEqualToView(self.headImageView)
    .bottomEqualToView(self.contentView)
    .rightEqualToView(self.contentView)
    .heightIs(1);
    
    
}

- (void)setModel:(LSExpertRankModel *)model{
    _model = model;
    
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.UserHeadUrl] placeholderImage:[UIImage imageNamed:@"MineDefaultHead"] options:SDWebImageRefreshCached];
    
//    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.UserHeadUrl] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//      
//    }];
    
    self.ExpertName.text = model.UserDisName;
    
    switch (model.type) {
        case 0:{
           self.redLable.text = [NSString stringWithFormat:@"%@连红",model.value];
        }
            break;
        case 1:{
           self.redLable.text = [NSString stringWithFormat:@"%@盈利率",model.value];
        }
            break;
        case 2:{
           self.redLable.text = [NSString stringWithFormat:@"%@胜率",model.value];
        }
            break;
        default:
            break;
    }
    
    if (model.IsAttended) {
        self.followBtn.selected = YES;
    }else{
        self.followBtn.selected = NO;
    }
    
    if ([model.UserCid isEqualToString:[LSAccountArchive account].Cid]) {
        self.followBtn.hidden = YES;
    }
}

- (void)clickFollowBtn:(UIButton *)sender{
//    sender.selected=!sender.selected;
    MyLog(@"cid -- %@",self.model.UserCid);
    if (self.clickFollowButtonBlock) {
        self.clickFollowButtonBlock(self.model.UserCid);
    }
}
@end


#pragma mark 🔥 LSRecommendExpertCell

@interface LSRecommendExpertCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation LSRecommendExpertCell

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.itemSize = CGSizeMake(LSScreenWidth/4, LSScreenWidth/4 + 20);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.width, self.contentView.height) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.scrollEnabled = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[LSExpertsCollectionViewCell class] forCellWithReuseIdentifier:@"expertsCollectionViewCell"];
        
    }
    return _collectionView;
}

+(LSRecommendExpertCell *)RecommendExpertCelllCellByTableView:(UITableView *)tableView{
    LSRecommendExpertCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass(self)];
    if (!cell) {
        cell=[[LSRecommendExpertCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(self)];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }return cell;
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addContentView];
    }
    
    return self;
}

-(void)addContentView{
    [self.contentView addSubview:self.collectionView];
    self.collectionView.sd_layout
    .leftEqualToView(self.contentView)
    .topEqualToView(self.contentView)
    .rightEqualToView(self.contentView)
    .bottomEqualToView(self.contentView);

    
}


- (void)setDataArray:(NSMutableArray *)dataArray{
    _dataArray = dataArray;
    [self.collectionView reloadData];
}
#pragma mark 🔥 UICollectionViewDelegate Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LSExpertsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"expertsCollectionViewCell" forIndexPath:indexPath];
    cell.clickFollowButtonBlock = ^(NSString *expertsCid) {
     
        if (self.clickFollowButtonBlock) {
            self.clickFollowButtonBlock(expertsCid);
        }
    };
    cell.model = _dataArray[indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MyLog(@"indePath : %zd",indexPath.row);
    
    
}

//- (void)clickFollowBtn:(UIButton *)sender{
//    sender.selected=!sender.selected;
//    if (self.clickFollowButtonBlock) {
//        self.clickFollowButtonBlock(sender.selected);
//    }
//}
@end

